(ns liberator-sample.core.tutorial.debugging
  (:require [liberator.core :refer [defresource resource]]
            [liberator.dev :refer [wrap-trace]]
            [compojure.core :refer [defroutes ANY]]
            [ring.middleware.params :refer [wrap-params]]
            [org.httpkit.server :as server]))


(declare dbg-resource dbg)
(def dbg-counter (atom 0))
(defresource dbg-resource
             :available-media-types ["text/plain"]
             :allowed-methods [:get :post]
             :handle-ok (fn [_] (format "The counter is %d" @dbg-counter))
             :post! (fn [_] (swap! dbg-counter inc)))

(defroutes dbg
           (ANY "/" []  "Hello")
           (ANY "/dbg-count" [] dbg-resource))

(def handler
  (-> dbg
      wrap-params
      (wrap-trace :header :ui)))

(server/run-server handler {:port 3333})